package com.ctsi.organise.app.rest;

import com.ctsi.auth.vcc.manager.VerificationCodeService;
import com.ctsi.framework.common.response.UnifiedResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author zhangjw
 * @className VericationCodeController
 * @description
 * @date 2019/2/22 8:29
 **/
@RestController
public class VericationCodeController {

    @Autowired
    private VerificationCodeService verificationCodeService;


    @GetMapping(value = "/valid-code/image/{token}")
    public void image(@PathVariable("token") String token, HttpServletResponse response) throws IOException {
        response.setContentType(MediaType.IMAGE_JPEG_VALUE);
        this.verificationCodeService.renderImage(token,response.getOutputStream());
    }

    @GetMapping(value = "/valid-code/token",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public UnifiedResponse<String> getToken(){
        String token = this.verificationCodeService.getToken(null,null,null,null);
        return UnifiedResponse.ok(token);
    }


}
